package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    /*
        添加购物车
            1）查询redis中的数据
         * 2）如果redis中已经有了，则追加数量，重新计算金额
         * 3）如果没有，将商品添加到缓存
     */
    @Override
    public void add(String skuId, Integer num, String username) {
        //从redis中获取购物项
        OrderItem  orderItem = (OrderItem) redisTemplate.boundHashOps("CAT_" + username).get(skuId);
        if(orderItem!=null){//redis中有购物项，则进行更新购物项，，
            //数量叠加
            orderItem.setNum(orderItem.getNum()+num);

            //商品删除
            if(orderItem.getNum()<=0){
                redisTemplate.boundHashOps("CAT_"+username).delete(skuId);
                return;
            }
            //总金额叠加
            orderItem.setMoney(orderItem.getNum()*orderItem.getPrice());
            //实付金额叠加
            orderItem.setPayMoney(orderItem.getNum()*orderItem.getPrice());
        }else{//redis中没有购物项，则进行添加到redis中
            //根据skuid获取sku，远程调用
            Sku sku = skuFeign.findById(skuId).getData();
            //根据skuid获取spu
            Spu spu = spuFeign.findSpuBySpuId(sku.getSpuId());
            //把spu和sku封装到orderItem类中
            orderItem=this.sku2OrderItem(sku,spu,num);
        }
        //最后，不管redis是否有购物项,都要进行更新redis
        redisTemplate.boundHashOps("CAT_"+username).put(skuId,orderItem);
    }



    /**
     * 根据用户查询购物车数据
     * @param username
     * @return
     */

    @Override
    public Map orderItemList(String username) {
        Map map=new HashMap();
        List<OrderItem> orderItemList = redisTemplate.boundHashOps("CAT_" + username).values();
        map.put("orderItemList",orderItemList);

        Integer totalNum=0;
        Integer totalMoney=0;
        for (OrderItem orderItem : orderItemList) {
            totalNum+=orderItem.getNum();
            totalMoney+=orderItem.getMoney();
        }
        map.put("totalNum",totalNum);
        map.put("totalMoney",totalMoney);
        return map;
    }

    //封装数据到orderItem中
    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
        OrderItem orderItem=new OrderItem();
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num*orderItem.getPrice());       //单价*数量
        orderItem.setPayMoney(num*orderItem.getPrice());    //实付金额
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight()*num);           //重量=单个重量*数量
        //分类ID设置
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        return orderItem;
    }
}